package registe.controller;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.client.RestTemplate;
import registe.bean.TUserInfo;
import registe.service.UserImpl;
import registe.service.UserInfoImpl;
import registe.untill.baseUntill.MessageUntill;
import registe.untill.baseUntill.YmlConfig;
import registe.untill.fileUntill.ImageFileUntill;
import registe.untill.httpDataUntill.HttpDataUntill;
import registe.bean.TUser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zhoumeineng on 2018/1/24.
 */

@RestController
@RequestMapping(value = "/registeSys/${version}",method = RequestMethod.POST)
public class RegisteSysController {

   @Autowired
    RestTemplate restTemplate;

   @Autowired
   YmlConfig ymlConfig;

    @Autowired
    UserImpl userImpl;


    @Autowired
    UserInfoImpl userInfoImpl;

    /**
     *
     * 验证手机 是否注册
     * */
    @RequestMapping(value = "/authAccount")
    public void AuthAccount(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        JSONObject jsonObject = userImpl.just_PhoneNum_Exit(result.getString("phoneNum"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }




    /**
     *
     * 验证短信验证码
     *
     * */


    @RequestMapping(value = "/authSMSCode")
    public void AuthSMSCode(){

    }




    /**
     *
     * 账号注册
     *
     * */
    @RequestMapping(value = "/accountRegist")
    public void accountRegist(HttpServletRequest req,HttpServletResponse rsp) throws Exception{
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        TUser tUser = new TUser();
        tUser.setPassword(result.getString("password"));
        tUser.setPhonenum(result.getString("phoneNum"));
        JSONObject jsonObject = userImpl.phoneNumRegist(tUser,result.getString("termId"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     *
     *  开通刷脸 录入 用户的真实信息
     *
     * */

    @RequestMapping(value = "/inputUserInfo")
    public void openFaceInputUserInfo(HttpServletRequest req,HttpServletResponse rsp) throws Exception{
        JSONObject result = HttpDataUntill.httpRspSet(req,rsp);
        TUserInfo tUserInfo = new TUserInfo();
        if (!result.has("uid")) {
            MessageUntill.ResboneOutPut(rsp,MessageUntill.messageBody(null,"0002","uid不能为空"));
            return;
        }

       /**
        * 判断 是否含有头像
        * */
        if (result.has("head")){
            tUserInfo.setHead(ymlConfig.getImagesPath()+"/head/"+result.getString("uid")+".jpg");
            /**
             * 图片写入static中
             * */
            ImageFileUntill.ImageOutput(ymlConfig.getImagesPath(),result.getString("head"),result.getString("uid")+".jpg","head");
        }

        /**
         *
         * 判断是否含有脸照片
         *
         * */
        if (result.has("face")){
            tUserInfo.setFace(ymlConfig.getImagesPath()+"/face/"+result.getString("uid")+".jpg");
            ImageFileUntill.ImageOutput(ymlConfig.getImagesPath(),result.getString("face"),result.getString("uid")+".jpg","face");
        }
        tUserInfo.setUid(result.getString("uid"));
        tUserInfo.setRealname(result.has("realName")?result.getString("realName"):null);
        tUserInfo.setIdnum(result.has("idNum")?result.getString("idNum"):null);
        JSONObject jsonObject = userInfoImpl.updataUserInfo(tUserInfo);
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /***
     *
     * 获取用户注册信息
     * 系统之间调用
     * */
    @RequestMapping(value = "/getUser")
    public void getUser(@RequestBody String body,HttpServletResponse rsp){
        rsp.setContentType("application/json;charset=UTF-8");
        JSONObject result =  new JSONObject(body);
        JSONObject jsonObject = userImpl.getUser(result.getString("phoneNum"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


    /**
     *  获取用户信息
     *  系统之间的调用
     * */
    @RequestMapping(value = "getUserInfo")
    public void getUserInfo(@RequestBody String body,HttpServletResponse rsp){
        rsp.setContentType("application/json;charset=UTF-8");
        JSONObject result =  new JSONObject(body);
        JSONObject jsonObject = userInfoImpl.getUserInfo(result.getString("uid"));
        MessageUntill.ResboneOutPut(rsp,jsonObject);
    }


}
